export default {
  namespaced: true,
  state: {
    user: {
      token: localStorage.getItem('jwt') || '',
      username: localStorage.getItem('username') || '',
      id:localStorage.getItem('id') || '',
      name: localStorage.getItem('name') || '',
    }
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.user.token = token
      localStorage.setItem('jwt', token)
    },
    SET_USERNAME(state, username) {
      state.user.username = username
      localStorage.setItem('username', username)
    },
    SET_ID(state, id) {
      state.user.id = id
      localStorage.setItem('id', id)
    },
    SET_NAME(state, name) {
      state.user.name = name
      localStorage.setItem('name', name)
    },
    CLEAR_TOKEN(state) {
      state.user.token = ''
      state.user.username = ''
      state.user.id = ''
      state.user.name = ''
      localStorage.removeItem('jwt')
      localStorage.removeItem('username')
      localStorage.removeItem('id')
      localStorage.removeItem('name')
    }
  },
  actions: {
    setUser({ commit }, { token, username ,id,name}) {
      commit('SET_TOKEN', token)
      commit('SET_USERNAME', username)
      commit('SET_ID', id)
      commit('SET_NAME', name)
    },
    logout({ commit }) {
      commit('CLEAR_TOKEN')
    }
  },
  getters: {
    isAuthenticated: state => !!state.user.token,
    getToken: state => state.user.token,
    getUsername: state => state.user.username,
    getUser: state => state.user,
    getId: state => state.user.id,
    getName: state => state.user.name
  }
}